<template>
  <div class="gender">
    <van-picker
  title="标题"
  show-toolbar
  :columns="columns"
  @confirm="onConfirm"
  @cancel="$emit('close')"
  @change="onChange"
  :default-index="value"
/>
  </div>
</template>

<script>
import { editUserinfo } from '@/api/user'
export default {
  name: 'gender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      genderIndex: this.value
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {

  },
  methods: {
    async onConfirm() {
      // 加载提示
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true,
        // 设置转动时间，为0就无限转动
        duration: 0
      })
      try {
        await editUserinfo({ gender: this.genderIndex })
        this.$emit('input', this.genderIndex)
        this.$toast.success('更新性别成功')
      } catch (err) {
        this.$toast.fail('更新性别失败')
      }
    },
    onChange(picker, value, index) {
      this.genderIndex = index
    }
  }
}
</script>

<style scoped lang="less">
</style>
